﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;

namespace WlanDesignDll
{
    class ScanAnalyzeCommandHandler : System.Windows.Input.ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            AcadApplication acAppComObj = null;
            const string strProgId = "AutoCAD.Application.18";
            // Get a running instance of AutoCAD
            try
            {
                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                }
                catch
                {
                    // If an instance of AutoCAD is not created then message and exit
                    System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                    return;
                }
            }
            AcadDocument acDocComObj;
            acDocComObj = acAppComObj.ActiveDocument;
            //acDocComObj.SendCommand("TestOn ");
            acDocComObj.SendCommand("ScanAnalyze ");
            //Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            //acDoc.SendStringToExecute("ScanAnalyze", true, false, false);
        }
    }
}
